import { validateOption, _support, setSilentFlag, generateUUID } from '../utils';
import { EVENTTYPES } from '../common';
import { breadcrumb } from './breadcrumb';
import { transportData } from './reportData';

export class Options {
  constructor() {
    this.dsn = 'http://localhost:8083/reportData';
    this.throttleDelayTime = 0; // click事件的节流时长
    this.overTime = 10; // 接口超时时长
    this.silentRecordScreen = false; // 是否开启录屏
    this.recordScreentime = 10; // 录屏时长
    // 录屏事件集合
    this.recordScreenTypeList = [
      EVENTTYPES.ERROR,
      EVENTTYPES.UNHANDLEDREJECTION,
      EVENTTYPES.RESOURCE,
      EVENTTYPES.FETCH, EVENTTYPES.XHR
    ]; 
    this.filterXhrUrlRegExp = RegExp; // 过滤的接口请求正则
    this.handleHttpStatus = null; // 处理接口返回的 response
    this.repeatCodeError = false; // 是否去除重复的代码错误，重复的错误只上报一次
  }
  bindOptions(options = {}) {
    const { 
      dsn,
      filterXhrUrlRegExp,
      throttleDelayTime,
      silentRecordScreen,
      overTime, recordScreenTypeList,
      recordScreentime,
      handleHttpStatus,
      repeatCodeError,
    } = options;
    validateOption(dsn, 'dsn', 'string') && (this.dsn = dsn);
    validateOption(throttleDelayTime, 'throttleDelayTime', 'number') && (this.throttleDelayTime = throttleDelayTime);
    validateOption(overTime, 'overTime', 'number') && (this.overTime = overTime);
    validateOption(recordScreentime, 'recordScreentime', 'number') && (this.recordScreentime = recordScreentime);
    if (validateOption(silentRecordScreen, 'silentRecordScreen', 'boolean')) {
      this.silentRecordScreen = silentRecordScreen;
      _support.recordScreenId = generateUUID(); // 添加初始的recordScreenId
    }
    validateOption(recordScreenTypeList, 'recordScreenTypeList', 'array') && (this.recordScreenTypeList = recordScreenTypeList);

    validateOption(filterXhrUrlRegExp, 'filterXhrUrlRegExp', 'regexp') && (this.filterXhrUrlRegExp = filterXhrUrlRegExp);
    validateOption(handleHttpStatus, 'handleHttpStatus', 'function') &&
      (this.handleHttpStatus = handleHttpStatus);
    validateOption(repeatCodeError, 'repeatCodeError', 'boolean') &&
      (this.repeatCodeError = repeatCodeError);
  }
}
const options = _support.options || (_support.options = new Options());

export function initOptions(paramOptions = {}) {
  // setSilentFlag 给全局添加已设置的标识，防止重复设置
  setSilentFlag(paramOptions);
  // 设置用户行为的配置项
  breadcrumb.bindOptions(paramOptions);
  // transportData 配置上报的信息
  transportData.bindOptions(paramOptions);
  // 绑定其他配置项
  options.bindOptions(paramOptions);
}
export { options };
